package com.hzsparrow.ai.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzsparrow.ai.entity.ModelConfig;
import com.hzsparrow.ai.mapper.ModelConfigMapper;
import com.hzsparrow.ai.config.AIModelInitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 模型配置服务实现类
 */
@Service
public class ModelConfigServiceImpl extends ServiceImpl<ModelConfigMapper, ModelConfig> implements ModelConfigService {

    @Autowired
    private AIModelInitService aiModelInitService;

    @Override
    public ModelConfig createModelConfig(ModelConfig modelConfig) {
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        modelConfig.setCreateTime(now);
        modelConfig.setUpdateTime(now);
        
        // 默认启用
        if (modelConfig.getEnabled() == null) {
            modelConfig.setEnabled(true);
        }
        
        // 保存模型配置
        this.save(modelConfig);
        return modelConfig;
    }

    @Override
    public ModelConfig getModelConfig(Long id) {
        return this.getById(id);
    }

    @Override
    public ModelConfig updateModelConfig(ModelConfig modelConfig) {
        // 设置更新时间
        modelConfig.setUpdateTime(LocalDateTime.now());
        
        // 更新模型配置
        this.updateById(modelConfig);
        
        // 通知AIModelInitService处理模型配置更新
        aiModelInitService.handleModelConfigUpdate(modelConfig);
        
        return modelConfig;
    }

    @Override
    public boolean deleteModelConfig(Long id) {
        // 获取要删除的模型配置信息
        ModelConfig modelConfig = this.getById(id);
        if (modelConfig != null) {
            // 先通知AIModelInitService处理模型配置删除
            aiModelInitService.handleModelConfigDelete(id, modelConfig.getModelType());
        }
        
        // 删除模型配置
        return this.removeById(id);
    }

    @Override
    public List<ModelConfig> getAllEnabledModels() {
        LambdaQueryWrapper<ModelConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ModelConfig::getEnabled, true)
                    .orderByAsc(ModelConfig::getSortOrder)
                    .orderByDesc(ModelConfig::getCreateTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<ModelConfig> getModelsByType(String modelType) {
        LambdaQueryWrapper<ModelConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ModelConfig::getModelType, modelType)
                    .eq(ModelConfig::getEnabled, true)
                    .orderByAsc(ModelConfig::getSortOrder)
                    .orderByDesc(ModelConfig::getCreateTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<ModelConfig> getModelsByCategory(String category) {
        LambdaQueryWrapper<ModelConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ModelConfig::getCategory, category)
                    .eq(ModelConfig::getEnabled, true)
                    .orderByAsc(ModelConfig::getSortOrder)
                    .orderByDesc(ModelConfig::getCreateTime);
        return this.list(queryWrapper);
    }

    @Override
    public Page<ModelConfig> listModelConfigs(int page, int size, String category, String modelType) {
        Page<ModelConfig> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<ModelConfig> queryWrapper = new LambdaQueryWrapper<>();
        
        // 根据分类过滤
        if (StringUtils.hasText(category)) {
            queryWrapper.eq(ModelConfig::getCategory, category);
        }
        
        // 根据模型类型过滤
        if (StringUtils.hasText(modelType)) {
            queryWrapper.eq(ModelConfig::getModelType, modelType);
        }
        
        queryWrapper.orderByAsc(ModelConfig::getSortOrder)
                    .orderByDesc(ModelConfig::getCreateTime);
        
        return this.page(pageParam, queryWrapper);
    }
} 